package com.xbw.controller;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjh.vo.Bis_bisiness_order;
import com.xbw.bean.Bis_Shop;
import com.xbw.bean.FinanceUser;
import com.xbw.bean.Home;
import com.xbw.service.FinanceService;
import com.zsy.bean.Bis_Shop_User;


@Controller
@RequestMapping("finance")
public class FinanceController {
    
	@Autowired
	private FinanceService fs;
	//鐠哄疇娴嗛崚鏉垮閸欐媽鍌ㄩ崝锟�
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request,@RequestParam(value="page",defaultValue="1")int page,HttpSession sess){
		ModelAndView md=new ModelAndView();
		Map<String, Object> map=remap(request,sess);
		 PageHelper.startPage(page, 5);
		 List<Bis_bisiness_order> list=fs.listbisinessordel(map);
		 PageInfo<Bis_bisiness_order> info=new PageInfo<>(list);
		 Bis_bisiness_order bo=fs.getorder(map);
		 md.addObject("info", info);
		 md.addObject("bo", bo);
		 md.addObject("map", map);
		 md.setViewName("finance/financelist");
		return  md; 	
		
	} 
	//妯＄硦灏佽
	private Map<String, Object>  remap(HttpServletRequest request,HttpSession sess){
		Map<String, Object> map=new HashMap<String,Object>();
		Bis_Shop_User bsu=(Bis_Shop_User)sess.getAttribute("shopuser");
		map.put("shop_user_id", bsu.getShop_user_id());
		map.put("shop_id", bsu.getShop_id());
		map.put("ptime", request.getParameter("ptime"));
		map.put("username", request.getParameter("username"));
		return map;
	}
	@RequestMapping("/export")//excel导出
	@ResponseBody
	public void report(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String arr=new String(request.getParameter("arr").getBytes("iso-8859-1"), "utf-8");  
		System.out.println("arr:"+arr);
		String []str=arr.split("&");
		String[]title=str[0].split(",");
		String fileName = "信息表"+System.currentTimeMillis()+".xls";
		String sheetName = "信息表";	
		String [][]content=new String[str.length][title.length];
		//String [][]content={str[1].split(","),str[2].split(",")};
		for(int i=0;i<str.length-1;i++){
			content[i]=str[i+1].split(",");
		}
		HSSFWorkbook wb = ExportUtils.getHSSFWorkbook(sheetName, title, content, null);
		try {
			this.setResponseHeader(response, fileName);
	       OutputStream os = response.getOutputStream();
	       wb.write(os);
	       os.flush();
	      os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
	//璺冲埌鍓嶅彴璐㈠姟鍛樺伐宸ヨ祫鍙婁笟缁�
	@RequestMapping("listuser")
	public ModelAndView listuser(HttpServletRequest request,@RequestParam(value="page",defaultValue="1")int page,HttpSession sess){
		ModelAndView md=new ModelAndView();
		Map<String, Object> map=remap(request,sess);
		 PageHelper.startPage(page, 5);
		 List<FinanceUser> list=fs.listfinanceuser(map);
		 PageInfo<FinanceUser> info=new PageInfo<>(list);
		  FinanceUser bo=fs.getfinanceuser(map);
		 md.addObject("info", info);
		 md.addObject("bo", bo);
		 md.addObject("map", map);
		 md.setViewName("finance/financeuser");
		return  md; 	
	} 
	//璺冲埌鍓嶅彴璐㈠姟
	@RequestMapping("info")
	public String info(){
		return  "finance/financeinfo"; 	
	} 
	
	//璺冲埌鍓嶅彴棣栭〉
	@RequestMapping("home")
	public ModelAndView home(HttpServletRequest request,HttpSession sess){
		ModelAndView md=new ModelAndView();	
		Map<String, Object> map=remap(request,sess);
		Bis_Shop_User bsu=fs.getuser(map);
		Bis_Shop_User bs=(Bis_Shop_User)sess.getAttribute("shopuser");
		 List<Home> list=fs.listhome(bs.getShop_user_id());
		 md.addObject("list", list);
		 md.addObject("bsu", bsu);
		 md.setViewName("index3");
		return md;
	}
    }


